/*
 * wueasy - A Java Distributed Rapid Development Platform.
 * Copyright (C) 2017-2022 wueasy.com , All Rights Reserved.

 * Without permission, no one is allowed to use 
 * (including but not limited to: copying, disseminating,
 * displaying, mirroring, uploading, downloading in illegal ways);

 * Otherwise, legal liability will be investigated according to law.
 */
package com.wueasy.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wueasy.base.vo.CaptchaVo;
import com.wueasy.base.vo.ResultVo;
import com.wueasy.base.vo.SessionVo;
import com.wueasy.cloud.web.annotation.Lock;
import com.wueasy.cloud.web.annotation.OperationLog;
import com.wueasy.demo.dto.EditUserInfoDto;
import com.wueasy.demo.dto.LoginDto;
import com.wueasy.demo.service.LoginService;

import cn.hutool.captcha.AbstractCaptcha;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.generator.RandomGenerator;
import jakarta.validation.Valid;
import reactor.core.publisher.Mono;

/**
 * 登录
 * 
 * @author: fallsea
 * @version 1.0
 */
@RestController
public class LoginController {

	@Autowired
	private LoginService loginService;

	/**
	 * 登录
	 * @author: fallsea
	 * @param dto
	 * @return
	 */
	@PostMapping(value = "/login", produces = MediaType.APPLICATION_JSON_VALUE)
	@Lock(keys = "#p0.accountNo")
	@OperationLog(name = "登录",content = "'欢迎登录系统,'+#p0.accountNo+',登录ip:'+#user.ip")
	public Mono<ResultVo<SessionVo>> login(@RequestBody @Valid LoginDto dto) {
		return Mono.just(ResultVo.ok(loginService.login(dto)));
	}
	
	/**
	 * 获取验证码
	 * @author: fallsea
	 * @return
	 */
	@PostMapping(value = "/captcha", produces = MediaType.APPLICATION_JSON_VALUE)
	public Mono<ResultVo<CaptchaVo>> captcha() {
		AbstractCaptcha captcha = CaptchaUtil.createLineCaptcha(150,60,5,150);
		captcha.setGenerator(new RandomGenerator("23456789ABCDEFGHJKLMNPQRSTUVWXYZ",5));
		String captchaCode = captcha.getCode();
		CaptchaVo vo = new CaptchaVo();
		vo.setCode(captchaCode);
		vo.setCaptcha(captcha.getImageBase64Data());
		return Mono.just(ResultVo.ok(vo));
	}
	
	
	/**
	 * 更新用户信息
	 * @author: fallsea
	 * @param dto
	 * @return
	 */
	@PostMapping(value= "/updateUserInfo",produces = MediaType.APPLICATION_JSON_VALUE)
	public Mono<ResultVo<SessionVo>> updateUserInfo(@RequestBody @Valid EditUserInfoDto dto)
	{
	    return Mono.just(ResultVo.ok(loginService.updateUserInfo(dto)));
	}
	
	
	@GetMapping(value= "/get",produces = MediaType.APPLICATION_JSON_VALUE)
	public Mono<ResultVo<String>> get(@RequestParam(value = "name",required = false)String name)
	{
	    return Mono.just(ResultVo.ok(name));
	}
}